package com.java.util;

import com.java.util.pojo.Detail;
import com.java.util.pojo.Overview;
import com.java.util.pojo.ComponentEnum;

import java.util.*;
import java.util.stream.Collectors;

/**
 * list集合中的list集合中的map集合的list分组
 */
public class ListOfMapOfListToMap {

    public static void main(String[] args) {

        List<Overview> resList = new ArrayList<>();

        Map<ComponentEnum, Set<String>> tempMap = resList
                .stream()
                // 拉平
                .flatMap(o -> o.getAssetGroupLanguageList()
                        .stream()
                        .flatMap(e -> e.getAssetGroupDetails().entrySet()
                                .stream()
                                // 映射成其他对象
                                .map(entry -> {
                                    Set<String> assetSet = entry.getValue()
                                            .stream()
                                            .map(Detail::getUuid)
                                            .collect(Collectors.toSet());

                                    // 再转成一个map
                                    return new AbstractMap.SimpleEntry<>(entry.getKey(), assetSet);
                                })))
                .collect(Collectors.groupingBy(
                        // 收集这个map的key
                        AbstractMap.SimpleEntry::getKey,

                        // 拉平收集这个map的value
                        Collectors.flatMapping(
                                entry -> entry.getValue().stream(),
                                Collectors.toSet()
                        )));

        // 计算ContentAdComponentType对应的一个集合的map数量
        Map<ComponentEnum, Integer> collect = resList
                .stream()
                .limit(1)
                .flatMap(o -> o.getAssetGroupLanguageList()
                        .stream()
                        .limit(1)
                        .flatMap(e -> {

                            Set<Map.Entry<ComponentEnum, List<Detail>>> entries = e.getAssetGroupDetails().entrySet();

                            return entries.stream();
                        }))
                .collect(Collectors.groupingBy(
                        Map.Entry::getKey,
                        Collectors.summingInt(entry -> entry.getValue().size())
                ));

    }
}
